package com.example.smartcity_poverty.ui.AllService.Attorneys.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.Attorneys.activity.AttMe.activity.AttMeAddActivity
import com.example.smartcity_poverty.ui.AllService.Attorneys.fragment.AttorneysHomeFragment
import com.example.smartcity_poverty.ui.AllService.Attorneys.fragment.AttorneysHomeFragment.Companion.AttId
import com.example.smartcity_poverty.ui.AllService.Attorneys.fragment.AttorneysMeFragment
import com.example.smartcity_poverty.ui.AllService.Attorneys.fragment.AttorneysMeFragment.Companion.MeId
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_attorneys_show.*
import kotlinx.android.synthetic.main.re_att_home_list_all.*
import kotlinx.coroutines.Dispatchers

/**
 * 律师详情页
 */
class AttorneysShowActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_attorneys_show)
        title = "律师详情页"




        val id = intent.getStringExtra("id")?.toInt()
        id?.let {
            coroutine(Dispatchers.IO){
                // 显示律师信息
                setUserAll(id)
            }
        }

        // 点击免费咨询
        attShowListUserAllBtn.setOnClickListener {
            startActivity(Intent(this, AttMeAddActivity::class.java).putExtra("id", id.toString()))
        }


        /**
         * TabLayout
         */
        val tab = attListShowTabLayout
        val vp = attListShowViewPager2

        val strName = listOf<String>("服务方式", "用户评价")
        tab.tabMode = TabLayout.MODE_SCROLLABLE
        for (i in strName){
            tab.addTab(tab.newTab().setText(i))
        }


        // ViewPager2适配器
        vp.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int = 2

            override fun createFragment(position: Int): Fragment
                = when(position){
                0 -> AttorneysHomeFragment()
                else -> AttorneysMeFragment()
            }

            override fun getItemId(position: Int): Long {
                when(position){
                    0 -> AttId = id!!
                    1 -> MeId = id!!
                }
                return super.getItemId(position)
            }
        }


        // TabLayout监听
        tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                vp.currentItem = tab?.position!!
            }

        })

        // ViewPager2监听
        vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                tab.getTabAt(position)?.select()
            }
        })

    }

    /**
     * 显示律师信息
     */
    private suspend fun setUserAll(id: Int) {
        val result = api.getAttUserAll(getToken(), id)
        // 填充信息
        if (result.code == 200){
            runOnUiThread {
                result.data?.let { it ->
                    glide(url + it.avatarUrl, attListShowImage)
                    attListShowName.text = it.name
                    attListShowTime.text = "工作起始时间: ${it.workStartAt}"
                    attListShowSize.text = "咨询人数: ${it.serviceTimes}人"
                    attListShowYes.text = "法律专长: ${it.legalExpertiseName}"
                    attListShowAll.text = "好评率: ${it.favorableCount}%"
                }
            }
        }else runOnUiThread { result.msg?.show() }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}